package com.etc.controller;

import com.etc.dto.DrugDto;
import com.etc.dto.Result;
import com.etc.entity.Drug;
import com.etc.service.DrugService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/drug")
public class DrugController {

    private  static  final Logger logger= LoggerFactory.getLogger(DrugController.class);

    @Autowired
    private DrugService drugService;

    @GetMapping("find")
    public @ResponseBody Result getDrugDetail(@RequestParam int pageSize,@RequestParam int pageNum,String drugName){
        logger.debug(drugName);
        Result result=new Result();
        PageInfo<Drug> drugDetail = drugService.getDrugDetail(drugName, pageNum, pageSize);
        if(!drugDetail.getList().isEmpty()){
            result.ok(drugDetail);
        }else{
            result.fail(0,"没有相关数据");
        }
        return result;
    }

    @PostMapping("/update")
    public @ResponseBody Result updateDrugDetail(@RequestParam int drugId, @RequestParam String drugName, @RequestParam int stock, @RequestParam BigDecimal price){
        Result result=new Result();
        boolean b = drugService.updateDrugDetail(drugId, drugName, stock,price);
        if(b){
            result.ok(b);
        }else {
            result.fail(0,"更改失败");
        }
        return result;
    }
}
